package com.fer.t1.client.widgets;

import java.util.LinkedList;
import java.util.Queue;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.UIObject;

public class LoadingWidget {

	private static PopupPanel popup;
	private static Image imgLoading;
	private static FlowPanel container;
	
	private static Queue<PopupPanel> popupQueue;
	
	public static void init(String imageLocation) {
		
		popup = new PopupPanel();
		
		container = new FlowPanel();
		popup.add(container);
		
		popup.setAnimationEnabled(true);
		popup.setModal(true);
		popup.setStyleName("popupPanelTransparent");
		popup.setGlassEnabled(true);
		popup.setGlassStyleName("popupGlass");
		
		imgLoading = new Image(imageLocation);
		container.add(imgLoading);
		
		popupQueue = new LinkedList<PopupPanel>();
	}
	
	public void show(UIObject uiObject)
	{
		popup.showRelativeTo(uiObject);
	}
	
	public static void show()
	{
		if (popupQueue == null)
		{
			popupQueue = new LinkedList<PopupPanel>();
		}
		if (popup!=null)
		{
			popup.show();
			popup.center();
		}
		popupQueue.add(popup);
	}
	
	public static void hide()
	{
		if (popupQueue.size()>0)
		{
			popupQueue.poll().hide();
		}
	}
}
